package com.youlu.campus.admin.base;

import com.youlu.campus.service.base.LockService;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

/**
 * 基础实现类
 *
 * @author: zhugz
 * @date: 2020/10/20 14:25
 */
@Slf4j
public abstract class BaseService {

    @Autowired
    protected MapperFacade mapperFacade;

    @Autowired
    protected MongoTemplate mongoTemplate;

    @Autowired
    protected LockService lockService;


    public <T> List<T> list(String key, Object value, Class<T> t) {
        Query query = new Query();
        query.addCriteria(Criteria.where(key).is(value));
        return mongoTemplate.find(query, t);
    }

    public <T> T find(String key, Object value, Class<T> t) {
        Query query = new Query();
        query.addCriteria(Criteria.where(key).is(value));
        return mongoTemplate.findOne(query, t);
    }
}
